﻿namespace WGController32_CSharp
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Net;
    using System.Windows.Forms;

    public class dfrmTCPIPConfigure : Form
    {
        private Button btnCancel;
        private Button btnOK;
        private CheckBox chkEditDateServer;
        private IContainer components;
        private int dataServerShortCycle = 4;
        private string dataServerShortIP = "";
        private int[] dataServerShortOption = new int[8];
        private int dataServerShortPort = 0xee4d;
        private GroupBox groupBox2;
        private GroupBox grpIP;
        private Label label1;
        private Label label197;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label label6;
        private Label label7;
        private Label label8;
        private Label label9;
        private NumericUpDown nudCycle;
        private RadioButton optDhcp;
        private RadioButton optSetIP;
        public string strGateway = "";
        public string strIP = "";
        public string strMac = "";
        public string strMask = "";
        public string strPCAddr = "";
        public string strSN = "";
        private TextBox txtf_ControllerSN;
        private TextBox txtf_gateway;
        private TextBox txtf_IP;
        private TextBox txtf_MACAddr;
        private TextBox txtf_mask;
        private TextBox txtHostIP;
        private TextBox txtPortShort;

        public dfrmTCPIPConfigure()
        {
            this.InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!this.txtf_ControllerSN.ReadOnly)
            {
                int num;
                this.txtf_ControllerSN.Text = this.txtf_ControllerSN.Text.Trim();
                if (!int.TryParse(this.txtf_ControllerSN.Text, out num))
                {
                    MessageBox.Show("Controller SN  Wrong");
                    return;
                }
            }
            this.txtf_IP.Text = this.txtf_IP.Text.Replace(" ", "");
            if (!this.isIPAddress(this.txtf_IP.Text))
            {
                MessageBox.Show("IP  Wrong");
            }
            else
            {
                this.txtf_mask.Text = this.txtf_mask.Text.Replace(" ", "");
                if (!this.isIPAddress(this.txtf_mask.Text))
                {
                    MessageBox.Show("mask  Wrong");
                }
                else
                {
                    this.txtf_gateway.Text = this.txtf_gateway.Text.Replace(" ", "");
                    if (!string.IsNullOrEmpty(this.txtf_gateway.Text) && !this.isIPAddress(this.txtf_gateway.Text))
                    {
                        MessageBox.Show("gateway  Wrong");
                    }
                    else
                    {
                        this.txtHostIP.Text = this.txtHostIP.Text.Replace(" ", "");
                        if (!string.IsNullOrEmpty(this.txtHostIP.Text) && !this.isIPAddress(this.txtHostIP.Text))
                        {
                            MessageBox.Show("HostIP  Wrong");
                        }
                        else
                        {
                            this.strSN = this.txtf_ControllerSN.Text;
                            this.strMac = this.txtf_MACAddr.Text;
                            this.strIP = this.txtf_IP.Text;
                            this.strMask = this.txtf_mask.Text;
                            this.strGateway = this.txtf_gateway.Text;
                            int num2 = 0;
                            WGPacketShort @short = new WGPacketShort {
                                iDevSn = long.Parse(this.strSN)
                            };
                            if (this.chkEditDateServer.Checked)
                            {
                                @short.Reset();
                                @short.functionID = 0x90;
                                if (!string.IsNullOrEmpty(this.txtHostIP.Text))
                                {
                                    Array.Copy(IPAddress.Parse(this.txtHostIP.Text).GetAddressBytes(), 0, @short.data, 0, 4);
                                }
                                int num3 = int.Parse(this.txtPortShort.Text);
                                @short.data[4] = (byte) (num3 & 0xff);
                                @short.data[5] = (byte) ((num3 >> 8) & 0xff);
                                @short.data[6] = (byte) this.nudCycle.Value;
                                if (@short.run(this.strPCAddr) != 1)
                                {
                                    MessageBox.Show("修改接收服务器失败...");
                                    return;
                                }
                            }
                            @short.Reset();
                            @short.functionID = 150;
                            Array.Copy(IPAddress.Parse(this.txtf_IP.Text).GetAddressBytes(), 0, @short.data, 0, 4);
                            Array.Copy(IPAddress.Parse(this.txtf_mask.Text).GetAddressBytes(), 0, @short.data, 4, 4);
                            if (!string.IsNullOrEmpty(this.strGateway))
                            {
                                Array.Copy(IPAddress.Parse(this.txtf_gateway.Text).GetAddressBytes(), 0, @short.data, 8, 4);
                            }
                            if (this.optDhcp.Checked)
                            {
                                @short.data[0] = 0;
                                @short.data[1] = 0;
                                @short.data[2] = 0;
                                @short.data[3] = 0;
                            }
                            @short.data[12] = 0x55;
                            @short.data[13] = 170;
                            @short.data[14] = 170;
                            @short.data[15] = 0x55;
                            num2 = @short.run(this.strPCAddr);
                            base.DialogResult = DialogResult.OK;
                            base.Close();
                        }
                    }
                }
            }
        }

        private long byteToLong(byte[] buff, int start, int len)
        {
            long num = 0L;
            for (int i = 0; (i < len) && (i < 4); i++)
            {
                long num3 = buff[i + start];
                num += num3 << (8 * i);
            }
            return num;
        }

        private void dfrmTCPIPConfigure_Load(object sender, EventArgs e)
        {
            this.txtf_ControllerSN.Text = this.strSN;
            this.txtf_MACAddr.Text = this.strMac;
            this.txtf_IP.Text = this.strIP;
            this.txtf_mask.Text = this.strMask;
            this.txtf_gateway.Text = this.strGateway;
            if (this.txtf_IP.Text == "255.255.255.255")
            {
                this.txtf_IP.Text = "192.168.0.0";
            }
            if (this.txtf_mask.Text == "255.255.255.255")
            {
                this.txtf_mask.Text = "255.255.255.0";
            }
            if (this.txtf_gateway.Text == "255.255.255.255")
            {
                this.txtf_gateway.Text = "";
            }
            if (this.txtf_gateway.Text == "0.0.0.0")
            {
                this.txtf_gateway.Text = "";
            }
            this.getdeviceNetInfo();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void getdeviceNetInfo()
        {
            int num = 0;
            WGPacketShort @short = new WGPacketShort {
                iDevSn = long.Parse(this.strSN)
            };
            @short.Reset();
            @short.functionID = 0x92;
            num = @short.run(this.strPCAddr);
            if (num == 1)
            {
                this.dataServerShortIP = $"{@short.recv[8]}.{@short.recv[9]}.{@short.recv[10]}.{@short.recv[11]}";
                this.dataServerShortPort = @short.recv[12] + (@short.recv[13] << 8);
                this.dataServerShortCycle = @short.recv[14];
                for (int i = 0; i < 8; i++)
                {
                    this.dataServerShortOption[i] = @short.recv[15 + i];
                }
                this.txtHostIP.Text = this.dataServerShortIP;
                this.txtPortShort.Text = this.dataServerShortPort.ToString();
                this.nudCycle.Value = this.dataServerShortCycle;
            }
            if (num == 1)
            {
                @short.Reset();
                @short.functionID = 0xf4;
                @short.data[0] = 0x55;
                @short.data[1] = 170;
                @short.data[2] = 170;
                @short.data[3] = 0x55;
                @short.data[4] = 0x92;
                @short.data[5] = 0;
                @short.data[6] = 0;
                num = @short.run(this.strPCAddr);
            }
            if (num == 1)
            {
                if (@short.recv[14] == 0xa5)
                {
                    this.optDhcp.Checked = true;
                }
                else
                {
                    this.optSetIP.Checked = true;
                }
            }
            this.grpIP.Enabled = this.optSetIP.Checked;
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(dfrmTCPIPConfigure));
            this.label1 = new Label();
            this.label2 = new Label();
            this.txtf_ControllerSN = new TextBox();
            this.txtf_MACAddr = new TextBox();
            this.label3 = new Label();
            this.txtf_IP = new TextBox();
            this.label4 = new Label();
            this.txtf_mask = new TextBox();
            this.label5 = new Label();
            this.txtf_gateway = new TextBox();
            this.btnOK = new Button();
            this.btnCancel = new Button();
            this.optDhcp = new RadioButton();
            this.optSetIP = new RadioButton();
            this.grpIP = new GroupBox();
            this.groupBox2 = new GroupBox();
            this.label6 = new Label();
            this.label7 = new Label();
            this.nudCycle = new NumericUpDown();
            this.label8 = new Label();
            this.txtPortShort = new TextBox();
            this.txtHostIP = new TextBox();
            this.label197 = new Label();
            this.chkEditDateServer = new CheckBox();
            this.label9 = new Label();
            this.grpIP.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.nudCycle.BeginInit();
            base.SuspendLayout();
            manager.ApplyResources(this.label1, "label1");
            this.label1.ForeColor = Color.Black;
            this.label1.Name = "label1";
            manager.ApplyResources(this.label2, "label2");
            this.label2.ForeColor = Color.Black;
            this.label2.Name = "label2";
            manager.ApplyResources(this.txtf_ControllerSN, "txtf_ControllerSN");
            this.txtf_ControllerSN.Name = "txtf_ControllerSN";
            this.txtf_ControllerSN.ReadOnly = true;
            this.txtf_ControllerSN.TabStop = false;
            manager.ApplyResources(this.txtf_MACAddr, "txtf_MACAddr");
            this.txtf_MACAddr.Name = "txtf_MACAddr";
            this.txtf_MACAddr.ReadOnly = true;
            this.txtf_MACAddr.TabStop = false;
            manager.ApplyResources(this.label3, "label3");
            this.label3.ForeColor = Color.Black;
            this.label3.Name = "label3";
            manager.ApplyResources(this.txtf_IP, "txtf_IP");
            this.txtf_IP.Name = "txtf_IP";
            manager.ApplyResources(this.label4, "label4");
            this.label4.ForeColor = Color.Black;
            this.label4.Name = "label4";
            manager.ApplyResources(this.txtf_mask, "txtf_mask");
            this.txtf_mask.Name = "txtf_mask";
            manager.ApplyResources(this.label5, "label5");
            this.label5.ForeColor = Color.Black;
            this.label5.Name = "label5";
            manager.ApplyResources(this.txtf_gateway, "txtf_gateway");
            this.txtf_gateway.Name = "txtf_gateway";
            this.btnOK.BackColor = Color.Transparent;
            manager.ApplyResources(this.btnOK, "btnOK");
            this.btnOK.ForeColor = Color.Black;
            this.btnOK.Name = "btnOK";
            this.btnOK.UseVisualStyleBackColor = false;
            this.btnOK.Click += new EventHandler(this.btnOK_Click);
            this.btnCancel.BackColor = Color.Transparent;
            manager.ApplyResources(this.btnCancel, "btnCancel");
            this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.ForeColor = Color.Black;
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.UseVisualStyleBackColor = false;
            manager.ApplyResources(this.optDhcp, "optDhcp");
            this.optDhcp.Name = "optDhcp";
            this.optDhcp.TabStop = true;
            this.optDhcp.UseVisualStyleBackColor = true;
            manager.ApplyResources(this.optSetIP, "optSetIP");
            this.optSetIP.Name = "optSetIP";
            this.optSetIP.TabStop = true;
            this.optSetIP.UseVisualStyleBackColor = true;
            this.optSetIP.CheckedChanged += new EventHandler(this.optSetIP_CheckedChanged);
            this.grpIP.Controls.Add(this.txtf_IP);
            this.grpIP.Controls.Add(this.label3);
            this.grpIP.Controls.Add(this.txtf_mask);
            this.grpIP.Controls.Add(this.label4);
            this.grpIP.Controls.Add(this.label5);
            this.grpIP.Controls.Add(this.txtf_gateway);
            manager.ApplyResources(this.grpIP, "grpIP");
            this.grpIP.Name = "grpIP";
            this.grpIP.TabStop = false;
            this.groupBox2.Controls.Add(this.label9);
            this.groupBox2.Controls.Add(this.label6);
            this.groupBox2.Controls.Add(this.label7);
            this.groupBox2.Controls.Add(this.nudCycle);
            this.groupBox2.Controls.Add(this.label8);
            this.groupBox2.Controls.Add(this.txtPortShort);
            this.groupBox2.Controls.Add(this.txtHostIP);
            this.groupBox2.Controls.Add(this.label197);
            manager.ApplyResources(this.groupBox2, "groupBox2");
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.TabStop = false;
            manager.ApplyResources(this.label6, "label6");
            this.label6.ForeColor = Color.Black;
            this.label6.Name = "label6";
            manager.ApplyResources(this.label7, "label7");
            this.label7.ForeColor = Color.Black;
            this.label7.Name = "label7";
            this.nudCycle.ForeColor = Color.Black;
            manager.ApplyResources(this.nudCycle, "nudCycle");
            int[] bits = new int[4];
            bits[0] = 0xfd;
            this.nudCycle.Maximum = new decimal(bits);
            this.nudCycle.Name = "nudCycle";
            int[] numArray2 = new int[4];
            numArray2[0] = 4;
            this.nudCycle.Value = new decimal(numArray2);
            manager.ApplyResources(this.label8, "label8");
            this.label8.ForeColor = Color.Black;
            this.label8.Name = "label8";
            this.txtPortShort.ForeColor = Color.Black;
            manager.ApplyResources(this.txtPortShort, "txtPortShort");
            this.txtPortShort.Name = "txtPortShort";
            this.txtHostIP.ForeColor = Color.Black;
            manager.ApplyResources(this.txtHostIP, "txtHostIP");
            this.txtHostIP.Name = "txtHostIP";
            manager.ApplyResources(this.label197, "label197");
            this.label197.ForeColor = Color.Black;
            this.label197.Name = "label197";
            manager.ApplyResources(this.chkEditDateServer, "chkEditDateServer");
            this.chkEditDateServer.BackColor = Color.Transparent;
            this.chkEditDateServer.ForeColor = Color.Black;
            this.chkEditDateServer.Name = "chkEditDateServer";
            this.chkEditDateServer.UseVisualStyleBackColor = false;
            manager.ApplyResources(this.label9, "label9");
            this.label9.ForeColor = Color.Black;
            this.label9.Name = "label9";
            base.CancelButton = this.btnCancel;
            manager.ApplyResources(this, "$this");
            base.Controls.Add(this.chkEditDateServer);
            base.Controls.Add(this.groupBox2);
            base.Controls.Add(this.label1);
            base.Controls.Add(this.grpIP);
            base.Controls.Add(this.label2);
            base.Controls.Add(this.optSetIP);
            base.Controls.Add(this.txtf_ControllerSN);
            base.Controls.Add(this.optDhcp);
            base.Controls.Add(this.txtf_MACAddr);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOK);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "dfrmTCPIPConfigure";
            base.Load += new EventHandler(this.dfrmTCPIPConfigure_Load);
            this.grpIP.ResumeLayout(false);
            this.grpIP.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.nudCycle.EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        public bool isIPAddress(string ipstr)
        {
            bool flag = false;
            try
            {
                if (string.IsNullOrEmpty(ipstr))
                {
                    return flag;
                }
                string[] strArray = ipstr.Split(new char[] { '.' });
                if (strArray.Length != 4)
                {
                    return flag;
                }
                flag = true;
                for (int i = 0; i <= 3; i++)
                {
                    int num;
                    if (!int.TryParse(strArray[i], out num))
                    {
                        flag = false;
                        break;
                    }
                    if ((num < 0) || (num > 0xff))
                    {
                        flag = false;
                        break;
                    }
                }
                if (int.Parse(strArray[0]) == 0)
                {
                    return false;
                }
                if (int.Parse(strArray[3]) == 0xff)
                {
                    flag = false;
                }
            }
            catch
            {
                flag = false;
            }
            return flag;
        }

        private void optSetIP_CheckedChanged(object sender, EventArgs e)
        {
            this.grpIP.Enabled = this.optSetIP.Checked;
        }
    }
}

